package com.duolaibei.business.view.goods.fragment;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.duolaibei.business.R;
import com.duolaibei.business.base.BaseFragment;
import com.duolaibei.business.utils.inject.Inject;
import com.duolaibei.business.view.goods.mode.RetailPriceBean;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class RetailPriceFragment extends BaseFragment {

    private List<RetailPriceBean> list;
    private RetailPriceAdapter retailPriceAdapter;
    @Inject(R.id.recycle_retail_barcode)
    private RecyclerView mRetailRecycle;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.fragment_price);
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        initRetailPriceList();
        setRetailPriceAdapter();
    }

    @Override
    protected void onSetListener() {

    }

    private void initRetailPriceList() {

        list = new ArrayList<>();
        RetailPriceBean retailPriceBean;
        for (int i = 0; i < 50; i++) {
            retailPriceBean = new RetailPriceBean();
            retailPriceBean.setFormats("规格" + i);
            retailPriceBean.setGoodsNum("1");
            retailPriceBean.setGoodsPrice("");
            retailPriceBean.setQrcodeStr("");
            list.add(retailPriceBean);
        }
    }

    private void setRetailPriceAdapter() {
        retailPriceAdapter = new RetailPriceAdapter(getActivity(), R.layout.item_retail_price, list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mRetailRecycle.setLayoutManager(layoutManager);
        Drawable drawable = ContextCompat.getDrawable(Objects.requireNonNull(getActivity()), R.drawable.shape_recycle_div);
        DividerItemDecoration decoration = new DividerItemDecoration(Objects.requireNonNull(getActivity()), DividerItemDecoration.VERTICAL);
        if (drawable != null) {
            decoration.setDrawable(drawable);
            mRetailRecycle.addItemDecoration(decoration);
        }
        mRetailRecycle.setAdapter(retailPriceAdapter);
    }

    private class RetailPriceAdapter extends CommonAdapter<RetailPriceBean> {

        public RetailPriceAdapter(Context context, int layoutId, List<RetailPriceBean> datas) {
            super(context, layoutId, datas);
        }

        @Override
        protected void convert(ViewHolder holder, RetailPriceBean retailPriceBean, int position) {
            holder.setText(R.id.txt_retail_price, retailPriceBean.getFormats());
        }
    }
}
